package text.text;

public class StudentNode<T> {
    Node<T> head = new Node();

    /**
     * 头插法
     */
    public void insertAtHead(T name) {
        Node inserthead = new Node();
        inserthead.name = name;
        inserthead.next = head.next;
        head.next = inserthead;
    }

    /**
     * 尾插法
     */
    public void insertAtTail(T name) {
        Node inserttail = head;
        Node tail = new Node();
        tail.name = name;
        if (inserttail.next == null) {
            tail.next = head.next;
            head.next = tail;
            return;
        } else {
            while (inserttail.next != null) {
                inserttail = inserttail.next;
            }
            tail.next = inserttail.next;
            inserttail.next = tail;
        }
    }

    /**
     * 任意位置，尾部插入法
     */
    public void insertAfter(T old,T name) {
        Node<T> node = head.next;
        Node alfer = new Node();
        alfer.name = name;
        while (node.next != null && !node.name.equals(old)) {
            node = node.next;
        }
        if (node == null) {
            System.out.println("找不到" + old + ",请输入有效的值");
            System.out.println();
            return;
        }
        alfer.next = node.next;
        node.next = alfer;

    }

    /**
     * 任意位置，头部插入法
     */
    public void insertBefor(T old, T name) {
        Node<T> node = head;
        while (node.next != null && !node.next.name.equals(old)) {
            node = node.next;
        }
        if (node.next == null) {
            System.out.println("找不到" + name + ",请输入有效的值");
            System.out.println();
            return;
        }
        Node alfer = new Node();
        alfer.name = name;
        alfer.next = node.next;
        node.next = alfer;
    }

    /**
     * 移除
     */
    public void remove(T name) {
        Node node = head;
        while (node.next != null && !node.next.name.equals(name)) {
            node = node.next;
        }
        if (node.next == null) {
            System.out.println("找不到" + name + ",请输入有效的值");
            System.out.println();
            return;
        }
        node.next = node.next.next;
    }

    /**
     * 清零
     */
    public void clean() {
        head.next = null;
        return;
    }

    /**
     * 获取多少个数
     */
    public int getCount() {
        int n = 0;
        Node count = head;
        while (!count.next.equals(null)) {
            count = count.next;
            n++;
        }
        return n;
    }

    /**
     * 打印
     */
    public void display() {

        if (head.next == null) {
            System.out.println("这是一张空表");
            return;
        } else {
            for (Node node = head.next; node != null; node = node.next) {
                System.out.print(node.name + "  ");
            }
            System.out.println();
        }
    }


}
